package gui;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

class GameScreen
        extends JComponent
{
    private static final long           serialVersionUID       = -4638355603617972723L;

    private BufferedImage               mDrawImage;
    private final GraphicsConfiguration mGraphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment()
                                                                       .getDefaultScreenDevice()
                                                                       .getDefaultConfiguration();
    
    private IPainter mPainter;

    public GameScreen(IPainter pPainter)
    {
        super();
        mPainter = pPainter;
    }

    @Override
    public void paint( Graphics pGraphics )
    {
        int width = getWidth();
        int height = getHeight();
        
        if( mDrawImage == null || mDrawImage.getWidth() != width || mDrawImage.getHeight() != height )
        {
            mDrawImage = mGraphicsConfiguration.createCompatibleImage(getWidth(), getHeight());
        }
        
        mPainter.draw(mDrawImage.createGraphics(), width, height);

        pGraphics.drawImage(mDrawImage, 0, 0, this);
    }

}
